<?php
/* 
 * class.friend.php
 * 我的家园-用户好友
 * @author samuel
 * @date 2009-05-25
 */
include_once 'class.myhomeBase.php';
class myhome_friend extends myhomeBase {

    function  __construct($core) {
        parent::__construct();
        //参数处理
        $this->step = $this->params['step'] ? strtolower(trim($core->params['step'])) : 'index';
	    $this->user_id = $this->params['uid'] ? (int)$this->params['uid'] : 1;
        $this->page = $this->params['page'] ? (int)$this->params['page'] : 1;
        $this->letter = $this->params['letter'] ? strtolower(trim($this->params['letter'])) : '';
        
	    if(!$this->user_id){
		    $this->error = "用户ID不合法！";
		    $this->jumpError();
		    exit;
	    }

        $run = "run_" . $this->step;
        
        //header("Content-type: text/vnd.wap.wml");
        $this->$run();
    }

    function run_index(){
    	//WAP页面
	    //header("Content-type: text/vnd.wap.wml");
	    //TITLE
	    $page_title = "我的家园-用户收藏";
	    $content = "";
	    
	    
	    $content .= "<img src='' alt='头像' /><br/>";
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?uid=".$this->user_id."'>动态</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userinfo&amp;uid=".$this->user_id."'>信息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;uid=".$this->user_id."'>设置</a><br/>";
        $content .= "<a href='".$_SERVER['PHP_SELF']."?act=msg&amp;uid=".$this->user_id."'>消息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."'>收藏</a>｜<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;uid=".$this->user_id."'>好友</a><br/>";
        $content .= "<a href='".$_SERVER['PHP_SELF']."?act=pubSeller&amp;uid=".$this->user_id."'>发布商家</a>｜<a href='".$_SERVER['PHP_SELF']."?act=pubDiscount&amp;uid=".$this->user_id."'>发布优惠</a><br/>";
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;uid=".$this->user_id."&amp;step=lookup'>寻找好友</a><br/>";
	    $content .= "ABCDEFGHIJKLM<br/>";
	    $content .= "NOPQRSTUVWXYZ<br/>";
	    
	    $pagesize = 10;
	    $displaypage = 5;
	    $page = $this->page;
	    $current_url = $_SERVER['PHP_SELF']."?act=friend&amp;uid=".$this->user_id;

        if(!$this->letter){
            $counts = $this->core->load('mycity::mycityBase')->getUserFriends($this->user_id, 1, true);
            $totalPages = ceil($counts/$pagesize);
            $pager = $this->core->utility->wap_pager($totalPages, $page, $displaypage, $current_url);
	        $arr_limit = array('offset'=>($page-1)*$pagesize, 'pagesize'=>$pagesize);
	        
	        $friends = $this->core->load('mycity::mycityBase')->getUserFriends($this->user_id, 1, false, $arr_limit);
	    }
	    //var_dump($friends);
	    $content .= $pager."<br/>";
	    if($friends){
	        foreach($friends as $friend){
	            $content .= "<img src='' alt='好友' /><a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=friendinfo&amp;fid=".$friend['UserID']."&amp;uid=".$this->user_id."'>".$friend['UserNickName']."</a><br/>";
	            $content .= "<a href=''>发短信</a><br/>";
	        }
	    }
	    $content .= $pager."<br/>";
	    
	    
	            

            

	    $content .= "<br/><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    }

    function run_friendinfo(){
        $fid = $this->params['fid'];
        $page_title = "我的家园-查看用户信息";
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&uid='.$this->user_id;
        $content = "";
            
        if(!$fid){
            $this->error = "对不起，您查看的用户不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        
        $content = "";
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&uid=' . $this->user_id;
        $content .= "<a href='".$backurl."'>返回上级</a><br/><br/>";
	    $content .= "<img src='' alt='头像' /><br/>";
        
        //好友用户信息
	    $friend = $this->core->load('user::userBase')->getInfo(array('table'=>'fun_user', 'primary'=>'UserID'), $fid);
	    //判断是否是好友
	    $tag_friend = $this->checkIsFriend($this->user_id, $fid);
	    //判断是否关注
	    $tag_attention = $this->checkIsAttention($this->user_id, $fid);
	    if($tag_attention){
	        $content .= "<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=delattention_do&amp;uid=".$this->user_id."&amp;fid=".$fid."'>取消关注</a>";
	    }else{
	        $content .= "<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=addattention_do&amp;uid=".$this->user_id."&amp;fid=".$fid."'>关注</a>";
	    }
	    
	    if($tag_friend){
	        $content .= " <a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=delfriend_do&amp;uid=".$this->user_id."&amp;fid=".$fid."'>删除好友</a> <a href=''>发短信</a><br/>";
	    }else{
	        $content .= " <a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=addfriend_do&amp;uid=".$this->user_id."&amp;fid=".$fid."'>加为好友</a><br/>";
	    }

	    
	    $content .= "金钱:".$friend['UserMoney']."<br/>";
	    $content .= "在线时间:".$friend['UserOnlineTime']."<br/>";
	    $content .= "上次登录时间:".$friend['UserLastLoginTime']."<br/>";
	    $content .= "房产<br/>";	
	    
	    //获取好友数
	    $friendsCount = $this->core->load('mycity::mycityBase')->getUserFriends($fid, 1 ,true);
	    //获取关注
	    $attenCount = $this->core->load('mytalk::mytalkBase')->getUserAttention('', $fid, true);
	
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=friendf&amp;fid=".$fid."&amp;uid=".$this->user_id."'>TA的好友(".(int)$friendsCount.")</a>|
                    <a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=friendatt&amp;fid=".$fid."&amp;uid=".$this->user_id."'>TA的粉丝(".(int)$attenCount.")</a><br/>";
	
	    $content .= "<br/><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";



	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
        
    }

    function run_lookup(){
        $page_title = "我的家园-寻找好友";
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&uid=' . $this->user_id;
        $posturl = $_SERVER['PHP_SELF'] . '?act=friend&step=addfriend_do&uid=' . $this->user_id;
        $content .= "<a href='".$backurl."'>返回上级</a><br/><br/>";
        
        $content .= "输入你好友手机号码，看看Ta是否也在找乐<br/>";
        $content .= "<input type='text' name='friend_mobile' value='' maxlength='20' size='15' />";
        $content .= "<anchor title=\"搜\">搜<go href=\"".$posturl."\" method=\"post\">
                        <postfield name=\"type\" value=\"friend\" />
                        <postfield name=\"friend_mobile\" value=\"$(friend_mobile)\" />
                        </go></anchor><br/><br/>";
        $content .= "你可能认识的人<br/>";
        $content .= "XXXXX<br/>XXXXX<br/>XXXXX<br/>XXXXX<br/>XXXXX<br/>";
        
        $content .= "<br/><br/>";
        $content .= "<a href='".$backurl."'>返回上级</a><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    }
    
    function run_delfriend_do(){
        $fid = $this->params['fid'];
        $page_title = "我的家园-删除好友";
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&step=friendinfo&fid='. $fid . '&uid=' . $this->user_id;
        $content = "";  
        if(!$fid){
            $this->error = "对不起，您删除的好友不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        //判断是否好友
        $tag_friend = $this->checkIsFriend($this->user_id, $fid);
        if(!$tag_friend){
            $this->error = "对不起，您没有该好友！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $res = $this->delUserFriend($this->user_id, $fid);
        if(!$res){
            $this->error = "对不起，您删除好友失败！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }else{
            $this->prompt = "成功删除好友！";
            $this->jumpPrompt("", $page_title, $backurl);
            exit;
        }
    }
    
    function run_addfriend_do(){
        $fid = $this->params['fid'];
        $page_title = "我的家园-添加好友";
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&step=friendinfo&fid='. $fid . '&uid=' . $this->user_id;
        $content = "";  
        if(!$fid){
            $this->error = "对不起，您要添加的好友不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        //判断是否好友
        $tag_friend = $this->checkIsFriend($this->user_id, $fid);
        if($tag_friend){
            $this->error = "对不起，TA已经是您的好友！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $res = $this->addUserFriend($this->user_id, $fid);
        if(!$res){
            $this->error = "对不起，您添加好友失败！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }else{
            $this->prompt = "成功添加好友！请等待对方验证！";
            $this->jumpPrompt("", $page_title, $backurl);
            exit;
        }
    }
    
    function run_delattention_do(){
        $fid = $this->params['fid'];
        $page_title = "我的家园-取消好友关注";
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&step=friendinfo&fid='. $fid . '&uid=' . $this->user_id;
        $content = "";  
        if(!$fid){
            $this->error = "对不起，您取消关注的好友不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        //判断是否关注对方
        $tag_attention = $this->core->load('mytalk::mytalkBase')->getUserAttention($this->user_id, $fid, $count=true);
        if(!$tag_attention){
            $this->error = "对不起，您没有关注对方！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $res = $this->core->load('mytalk::mytalkBase')->delUserAttention($this->user_id, $fid);
        if(!$res){
            $this->error = "对不起，您取消关注好友失败！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }else{
            $this->prompt = "成功取消关注好友！";
            $this->jumpPrompt("", $page_title, $backurl);
            exit;
        }
    }
    
    
    function run_addattention_do(){
        $fid = $this->params['fid'];
        $page_title = "我的家园-关注好友";
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&step=friendinfo&fid='. $fid . '&uid=' . $this->user_id;
        $content = "";  
        if(!$fid){
            $this->error = "对不起，您要关注的好友不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        //判断是否关注对方
        $tag_attention = $this->core->load('mytalk::mytalkBase')->getUserAttention($this->user_id, $fid, $count=true);
        if($tag_attention){
            $this->error = "对不起，TA已经是您关注的好友！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $res = $this->core->load('mytalk::mytalkBase')->addUserAttention($this->user_id, $fid);
        if(!$res){
            $this->error = "对不起，您关注好友失败！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }else{
            $this->prompt = "成功关注好友！";
            $this->jumpPrompt("", $page_title, $backurl);
            exit;
        }
    }
    
    function run_friendf(){
        $fid = $this->params['fid'];
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&step=friendinfo&fid='. $fid . '&uid=' . $this->user_id;

        $page_title = "我的家园-好友的好友";
        $content = "";

        $pagesize = 10;
	    $displaypage = 5;
	    $page = $this->page;
	    $current_url = $_SERVER['PHP_SELF']."?act=friend&amp;step=friendf&amp;fid=".$fid."&amp;uid=".$this->user_id;

        $counts = $this->core->load('mycity::mycityBase')->getUserFriends($fid, 1, true);
        $totalPages = ceil($counts/$pagesize);
        $pager = $this->core->utility->wap_pager($totalPages, $page, $displaypage, $current_url);
        $arr_limit = array('offset'=>($page-1)*$pagesize, 'pagesize'=>$pagesize);

        $friends = $this->core->load('mycity::mycityBase')->getUserFriends($fid, 1, false, $arr_limit);

	    //var_dump($friends);
	    $content .= $pager."<br/>";
	    if($friends){
	        foreach($friends as $friend){
	            $content .= "<img src='' alt='好友' /><a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=friendinfo&amp;fid=".$friend['UserID']."&amp;uid=".$this->user_id."'>".$friend['UserNickName']."</a><br/>";
	        }
	    }
	    $content .= $pager."<br/>";

        $content .= "<a href='".$backurl."'>返回上级</a><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

        $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    }

    function run_friendatt(){
        $fid = $this->params['fid'];
        $backurl = $_SERVER['PHP_SELF'] . '?act=friend&step=friendinfo&fid='. $fid . '&uid=' . $this->user_id;

        $page_title = "我的家园-好友的粉丝";
        $content = "";

        $pagesize = 10;
	    $displaypage = 5;
	    $page = $this->page;
	    $current_url = $_SERVER['PHP_SELF']."?act=friend&amp;step=friendatt&amp;fid=".$fid."&amp;uid=".$this->user_id;

        $counts = $this->core->load('mytalk::mytalkBase')->getUserAttention("", $fid, true);
        $totalPages = ceil($counts/$pagesize);
        $pager = $this->core->utility->wap_pager($totalPages, $page, $displaypage, $current_url);
        $arr_limit = array('offset'=>($page-1)*$pagesize, 'pagesize'=>$pagesize);

        $friends = $this->core->load('mytalk::mytalkBase')->getUserAttention("", $fid, false, "", $arr_limit);

	    //var_dump($friends);
	    $content .= $pager."<br/>";
	    if($friends){
	        foreach($friends as $friend){
                $user = $this->core->load('user::userBase')->getInfo(array('table'=>'fun_user', 'primary'=>'UserID'), $friend['UserID']);
	            $content .= "<img src='' alt='粉丝' /><a href='".$_SERVER['PHP_SELF']."?act=friend&amp;step=friendinfo&amp;fid=".$user['UserID']."&amp;uid=".$this->user_id."'>".$user['UserNickName']."</a><br/>";
	        }
	    }
	    $content .= $pager."<br/>";

        $content .= "<a href='".$backurl."'>返回上级</a><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

        $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    }
    

}


?>
